package com.bizdk.fastfx.fxsupport;

import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.net.URL;
import java.util.ResourceBundle;

/**
 * fxml controller 的父类
 */
public abstract class JavaFxController {
    public abstract void initialize(URL location, ResourceBundle resources);

    private Parent root;

    private Stage stage;

    private Scene scene;

    public Parent getRoot() {
        if (null == root) {
            FxmlView fxmlView = this.getClass().getAnnotation(FxmlView.class);
            this.root = FxmlViewLoader.load(fxmlView);
        }
        return this.root;
    }

    public Stage getStage() {
        return stage;
    }

    public void setStage(Stage stage) {
        StageManager.register(stage);
        this.stage = stage;
    }

    public Scene getScene() {
        return scene;
    }

    public void setScene(Scene scene) {
        this.scene = scene;
    }

    public void show() {
        this.stage = new Stage();
        this.scene = new Scene(getRoot());
        this.stage.setScene(this.scene);
        this.stage.centerOnScreen();
        this.stage.show();
    }

    public void show(Stage stage) {
        this.stage = stage;
        this.scene = new Scene(getRoot());
        this.stage.setScene(this.scene);
        this.stage.show();
    }

    public void show(Scene scene) {
        this.scene = scene;
        this.scene.setRoot(getRoot());
        this.stage.show();
    }
}
